package com.itheima.youdianda.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.exception.CommonException;
import com.itheima.youdianda.mapper.YddAdvimgMapper;
import com.itheima.youdianda.mapper.YddadvposMapper;
import com.itheima.youdianda.pojo.YddAdvimg;
import com.itheima.youdianda.pojo.YddAdvpos;
import com.itheima.youdianda.service.YddAdvimgService;
import com.itheima.youdianda.service.YddadvposService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvimg> implements YddAdvimgService {
    @Autowired
    private YddadvposMapper yddadvposMapper;

    /**
     * 广告图详情
     *
     * @return
     */
    @Override
    public R rowInfo(Integer id) {
        YddAdvimg yddAdvimg = this.getById(id);
        if (ObjectUtil.isEmpty(yddAdvimg)) {
            throw new CommonException("系统中无数据");
        }
        return R.success(yddAdvimg);

    }

    /**
     * 广告图列表分页
     *
     * @param page
     * @param limit
     * @param advimgpos
     * @param advimgdesc
     * @return
     */
    @Override
    public R yddAdvimgList(Integer page, Integer limit, String advimgpos, String advimgdesc) {
        Page<YddAdvimg> yddAdvimgPage = new Page<>(page, limit);

        //添加条件
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(advimgdesc), YddAdvimg::getAdvimgdesc, advimgdesc);
        queryWrapper.like(StrUtil.isNotBlank(advimgpos), YddAdvimg::getAdvimgpos, advimgpos);
        //分页查询
        this.page(yddAdvimgPage, queryWrapper);
        List<YddAdvimg> yddAdvimgList = yddAdvimgPage.getRecords();

        //将分页后的数据补充
        for (YddAdvimg yddAdvimg : yddAdvimgList) {
            //获取关联地址
            String advimgpos1 = yddAdvimg.getAdvimgpos();
            Integer yddAdvposId = Convert.toInt(advimgpos1);
            LambdaQueryWrapper<YddAdvpos> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(yddAdvposId != null, YddAdvpos::getId, yddAdvposId);
            YddAdvpos yddAdvpos = yddadvposMapper.selectOne(wrapper);
            yddAdvimg.setAdvposname(yddAdvpos.getAdvposname());
        }
        yddAdvimgPage.setRecords(yddAdvimgList);

        return R.success(yddAdvimgPage);
    }

    /**
     * 广告图添加
     *
     * @param yddAdvimg
     * @return
     */
    @Override
    public R yddAdvimgAdd(YddAdvimg yddAdvimg) {
        //设置默认值
        //yddAdvimg.setDefaultData("1");

        this.save(yddAdvimg);
        return R.success("添加成功");
    }

    /**
     * 广告图修改
     *
     * @param yddAdvimg
     * @return
     */
    @Override
    public R yddAdvimgUpdate(YddAdvimg yddAdvimg) {
        this.updateById(yddAdvimg);
        return R.success("修改成功");
    }

    /**
     * 广告图删除
     *
     * @param id
     * @return
     */
    @Override
    public R del(Integer id) {
        YddAdvimg yddAdvimg = this.getById(id);
        //获取默认值
        String defaultData = yddAdvimg.getDefaultData();
        //若默认值为1,不可以删除
        if ("1".equals(defaultData)) {
            return R.error("系统默认数据，不允许删除");
        }
        //删除
        this.removeById(id);
        return R.success("删除成功!");
    }
}
